<template>
	<div>
		<navigation-bar>
			<div class="searchContent">
				<el-input
					v-model="drugSeq"
					placeholder="请输入给药序号"
					size="mini"
					style="width: 150px; margin-right: 10px"
				></el-input>
				<custom-button
					type="search"
					style="margin-right: 10px"
					@searchClick="searchClick"
				></custom-button>
				<custom-button
					type="reset"
					style="margin-right: 10px"
					@resetClick="resetClick"
				></custom-button>
				<custom-button
					type="add"
					@addClick="addClick"
					style="margin-right: 10px"
				></custom-button>
				<custom-button
					type="add"
					title="同步"
					@addClick="synchronousClick"
				></custom-button>
			</div>
		</navigation-bar>
		<el-row>
			<el-col :span="6">
				<div style="width: 98%">
					<div class="btns">
						<navigation-bar-custom customTitle="检查配置">
							<div class="searchContent">
								<custom-button
									type="custom"
									icon="el-icon el-icon-refresh"
									title="刷新"
									style="margin-right: 10px"
									@customClick="refreshLeftData"
								></custom-button>
								<custom-button
									type="add"
									title="新增"
									@addClick="leftAddClick"
								></custom-button>
							</div>
						</navigation-bar-custom>
					</div>
					<single-table
						:dataSource="leftDataSource"
						:tableColumns="leftTableColumns"
						@handleSelectionChange="singleHandleSelectionChange"
						:selection="true"
						:height="curHeight"
						:btnList="letBtnList"
						@editRow="leftEditRow"
						@deleteRow="leftDeleteRow"
					></single-table>
				</div>
			</el-col>
			<el-col :span="18">
				<table-index
					:dataSource="dataSource"
					:tableColumns="tableColumns"
					:btnList="btnList"
					:pageSize="pageSize"
					:pageCount="pageCount"
					:pageNo="pageNo"
					@pageChange="pageChange"
					@editRow="editRow"
					@viewRow="viewRow"
					@deleteRow="deleteRow"
				></table-index>
			</el-col>
		</el-row>
		<div v-if="dialogShow">
			<custom-dialog
				:title="dialogTitle"
				size="large"
				@dialogClose="dialogClose"
			>
				<form-index :formConfig="formConfig" @submit="submit" :type="type">
					<!--  <div>这里还可以插入其它内容进行拓展</div> -->
				</form-index>
			</custom-dialog>
		</div>

		<div v-if="leftDialogShow">
			<custom-dialog
				:title="leftDialogTitle"
				size="large"
				@dialogClose="leftDialogClose"
			>
				<form-index
					ref="leftForm"
					:formConfig="leftFormConfig"
					@submit="leftSubmit"
					:type="leftType"
					@formCheckChange="formCheckChange"
				>
					<!--  <div>这里还可以插入其它内容进行拓展</div> -->
				</form-index>
			</custom-dialog>
		</div>

		<div v-if="periodDialogShow">
			<custom-dialog
				title="同步周期数据"
				size="small"
				@dialogClose="periodDialogClose"
			>
				<period-no-select
					stage="Ing"
					cfgType="OtherCheck"
					@periodSelectClose="periodDialogClose"
				></period-no-select>
			</custom-dialog>
		</div>
	</div>
</template>

<script>
	import { OtherCheckConfigurationsFormConfig } from "@/assets/form/managementCenter/testConfiguration/occupancyConfiguration/otherCheckConfigurationsForm"
	import { OtherInspectionTimePointsFormConfig } from "@/assets/form/managementCenter/testConfiguration/testPeriodConfiguration/otherInspectionTimePointsForm"
	import customDialog from "@/components/customComponents/customDialog"
	import customButton from "@/components/customComponents/customForm/customButton"
	import formIndex from "@/components/customComponents/customForm/formIndex"
	import singleTable from "@/components/customComponents/customeTable/tableHeightIndex"
	import tableIndex from "@/components/customComponents/customeTable/tableIndex"
	import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
	import navigationBarCustom from "@/components/customComponents/managementCenter/navigationBarCustom"
	import periodNoSelect from "@/components/customComponents/periodNoSelect"
	import { AjaxCommon } from "@/libs/apiCommon"
	import { PopupCommon } from "@/libs/popupCommon"
	import { SessionCommon } from "@/libs/sessionCommon"
	export default {
		components: {
			navigationBar,
			navigationBarCustom,
			customButton,
			tableIndex,
			singleTable,
			customDialog,
			formIndex,
			periodNoSelect,
		},
		data() {
			return {
				curHeight: 100,
				leftType: "add",
				leftDialogTitle: "",
				leftDialogShow: false,
				periodDialogShow: false,
				leftSelectIds: [],
				leftDataSource: [],
				leftTableColumns: [
					{
						prop: "name",
						label: "检查名称",
						// twoColumns: ["typeName", "testIndex"],
					},
				],
				drugSeq: "",
				// table 相关参数  begin
				dataSource: [],
				tableColumns: [
					{
						prop: "periodNo",
						label: "周期号",
					},
					{
						prop: "drugNo",
						label: "基于给药序号",
						width: 120,
					},
					{
						prop: "baseDrugTime",
						label: "基于给药时间",
						width: 120,
						dictName: "base_time",
					},
					{
						prop: "timePoint",
						label: "时间点",
					},
					{
						prop: "oppositeDay",
						label: "相对天数",
					},
					{
						prop: "oppositeTime",
						label: "相对时间",
					},
					{
						prop: "ttwSizeof",
						label: "时间窗运算符",
						width: 120,
					},
					{
						prop: "ttwValue",
						label: "时间窗值",
						width: 120,
					},
					{
						prop: "ttwUnit",
						label: "时间窗单位",
						width: 120,
					},
					{
						prop: "remarks",
						label: "监测点描述",
						width: 120,
					},
					{
						prop: "correctPlan",
						label: "是否矫正计划时间",
						width: 180,
					},
				],
				letBtnList: ["edit", "delete"],
				btnList: ["edit", "view", "delete"],
				pageSize: 20,
				pageCount: 100,
				pageNo: 1,
				// table 相关参数  end

				// 弹窗是否显示
				dialogShow: false,
				// 弹窗标题 (新增修改不同)
				dialogTitle: "",

				// form的配置信息
				formConfig: OtherInspectionTimePointsFormConfig,
				leftFormConfig: OtherCheckConfigurationsFormConfig,
				type: "add",
			}
		},
		methods: {
			getHeight() {
				this.curHeight = window.innerHeight - 220
			},
			refreshLeftData() {
				this.initLeftData()
			},
			periodDialogClose() {
				this.periodDialogShow = false
			},
			synchronousClick() {
				this.periodDialogShow = true
			},
			formCheckChange(e, name) {
				console.log(name)
				const formValue = this.$refs.leftForm.getFormValue()
				formValue.optionNum = e.value.length
				this.$refs.leftForm.changeFormValue(formValue)
			},
			leftSubmit(d) {
				let self = this
				if (d.id) {
					AjaxCommon("/ccdc/cfg/exp/ing/expIngOtherCheck/edit", "post", d)
						.then((res) => {
							if (res.data.code == 200) {
								PopupCommon.notify("success", "操作成功")
								self.initLeftData()
								self.leftDialogClose()
							} else {
								PopupCommon.notify("error", res.data.message)
							}
						})
						.catch((err) => {
							PopupCommon.notify("error", err.message)
						})
				} else {
					delete d.id
					AjaxCommon("/ccdc/cfg/exp/ing/expIngOtherCheck/add", "post", d)
						.then((res) => {
							if (res.data.code == 200) {
								PopupCommon.notify("success", "操作成功")
								self.initLeftData()
								self.leftDialogClose()
							} else {
								PopupCommon.notify("error", res.data.message)
							}
						})
						.catch((err) => {
							PopupCommon.notify("error", err.message)
						})
				}
			},
			submit(d) {
				let self = this
				d.correctPlan = d.correctPlan ? 1 : 0
				if (d.id) {
					AjaxCommon(
						"/ccdc/cfg/exp/ing/expIngOtherCheckTimePoint/edit",
						"post",
						d
					)
						.then((res) => {
							if (res.data.code == 200) {
								PopupCommon.notify("success", "操作成功")
								self.initPage()
								self.dialogClose()
							} else {
								PopupCommon.notify("error", res.data.message)
							}
						})
						.catch((err) => {
							PopupCommon.notify("error", err.message)
						})
				} else {
					delete d.id
					AjaxCommon(
						"/ccdc/cfg/exp/ing/expIngOtherCheckTimePoint/add",
						"post",
						d
					)
						.then((res) => {
							if (res.data.code == 200) {
								PopupCommon.notify("success", "操作成功")
								self.initPage()
								self.dialogClose()
							} else {
								PopupCommon.notify("error", res.data.message)
							}
						})
						.catch((err) => {
							PopupCommon.notify("error", err.message)
						})
				}
			},
			dialogClose() {
				this.dialogShow = false
			},
			leftDialogClose() {
				this.leftDialogShow = false
			},
			searchClick() {
				this.pageNo = 1
				this.initPage()
			},
			resetClick() {
				this.drugSeq = ""
			},
			leftAddClick() {
				let formConfigItem = this.leftFormConfig
				formConfigItem.map((t) => {
					Object.keys(t).map((res) => {
						if (res == "fieldName") {
							t.filedValue = ""
						}
					})
				})
				this.leftFormConfig = formConfigItem
				this.leftType = "add"
				this.leftDialogShow = true
				this.leftDialogTitle = "新增检查配置"
			},
			addClick() {
				if (this.leftSelectIds.length != 1) {
					PopupCommon.notify("info", "请勾选一个检查名称")
					return
				}
				let formConfigItem = this.formConfig
				formConfigItem.map((t) => {
					Object.keys(t).map((res) => {
						if (res == "fieldName") {
							t.filedValue = ""
						}
					})
				})
				// formConfigItem.expId = this.leftSelectIds[0].id
				this.formConfig = formConfigItem

				this.type = "add"
				this.dialogShow = true
				this.dialogTitle =
					"新增" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
			},
			// 分页点击事件
			pageChange(p) {
				this.pageNo = p
				this.initPage()
			},
			leftDeleteRow(d) {
				const id = d.data.id
				let self = this
				AjaxCommon(
					"/ccdc/cfg/exp/ing/expIngOtherCheck/delete?id=" + id,
					"delete",
					{ optReason: d.optReason }
				).then((res) => {
					console.log(res, "res...............")
					if (res.data.code == 200) {
						PopupCommon.notify("success", "删除成功")
						self.initLeftData()
						self.leftDialogClose()
					} else {
						PopupCommon.notify("error", res.data.message)
					}
				})
			},
			leftEditRow(d) {
				this.leftType = "edit"
				let formConfigItem = this.leftFormConfig
				formConfigItem.map((t) => {
					Object.keys(t).map((res) => {
						if (res == "fieldName") {
							t.filedValue = d.data[t[res]]
						}
					})
				})
				this.leftFormConfig = formConfigItem
				this.leftDialogShow = true
				this.leftDialogTitle = "编辑检查配置"
			},
			editRow(d) {
				this.type = "edit"
				let formConfigItem = this.formConfig
				formConfigItem.map((t) => {
					Object.keys(t).map((res) => {
						if (res == "fieldName") {
							t.filedValue = d.data[t[res]]
						}
					})
				})

				formConfigItem.correctPlan = d.correctPlan == 1

				this.formConfig = formConfigItem
				this.dialogShow = true
				this.dialogTitle =
					"编辑" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
			},
			viewRow(d) {
				this.type = "look"
				let formConfigItem = this.formConfig
				formConfigItem.map((t) => {
					Object.keys(t).map((res) => {
						if (res == "fieldName") {
							t.filedValue = d.data[t[res]]
						}
					})
				})
				this.formConfig = formConfigItem
				this.dialogShow = true
				this.dialogTitle =
					"查看" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
			},
			deleteRow(d) {
				const id = d.data.id
				let self = this
				AjaxCommon(
					"/ccdc/cfg/exp/ing/expIngOtherCheckTimePoint/delete?id=" + id,
					"delete",
					{ optReason: d.optReason }
				).then((res) => {
					console.log(res, "res...............")
					if (res.data.code == 200) {
						PopupCommon.notify("success", "删除成功")
						self.initPage()
						self.dialogClose()
					} else {
						PopupCommon.notify("error", res.data.message)
					}
				})
			},
			// 左侧选中事件
			singleHandleSelectionChange(d) {
				console.log(d)
				this.leftSelectIds = d
			},
			// 初始化事件
			initPage() {
				let self = this
				AjaxCommon(
					"/ccdc/cfg/exp/ing/expIngOtherCheckTimePoint/listByPage",
					"post",
					{
						drugSeq: this.drugSeq,
						pageNo: this.pageNo,
						pageSize: this.pageSize,
					}
				).then((res) => {
					if (res.data.code == 200) {
						self.dataSource = res.data.result.records
						self.pageCount = res.data.result.total
					} else {
						PopupCommon.notify("error", res.data.message)
					}
				})
			},
			initLeftData() {
				AjaxCommon(
					"/ccdc/cfg/exp/ing/expIngOtherCheck/listByPage",
					"post",
					{}
				).then((res) => {
					console.log(res, ".......................")
					if (res.data.code == 200) {
						this.leftDataSource = res.data.result.records
					} else {
						this.leftDataSource = []
						PopupCommon.notify("error", res.data.message)
					}
				})
			},
			// 初始化事件
			initSelect() {
				AjaxCommon("/ccdc/businessSelect/drugTime", "post", {}).then((res) => {
					if (res.data.code == 200) {
						this.formConfig = OtherInspectionTimePointsFormConfig(
							res.data.result
						)
					} else {
						PopupCommon.notify("error", res.data.message)
					}
				})
			},
		},
		mounted() {
			this.$nextTick(function () {
				this.getHeight()
			})
			/*窗口自适应，关键代码*/
			window.onresize = () => {
				this.getHeight()
			}
			this.initPage()
			this.initLeftData()
			this.initSelect()
		},
	}
</script>

<style scoped>
	.btns {
		text-align: right;
	}
</style>
